package com.iespring.util.map;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;

public class ActionTypeUtils {
    static HashMap<Integer, ActionMap> map;
    static {
        map = new HashMap<>();
        map.put(0,new ActionMap("admin/storage", "一般操作"));
        map.put(1,new ActionMap("auth", "安全操作"));
        map.put(2,new ActionMap("admin/order", "订单操作"));
        map.put(3,new ActionMap("*", "其他操作"));
    }
    public static Integer getType(String api){
        AtomicReference<Integer> type = new AtomicReference<>(map.size() - 1);
        map.entrySet().stream().forEach(
                set -> {
                    if (api.contains(set.getValue().getKey())) {
                        type.set(set.getKey());
                    }
                }
        );
        return type.get();
    }
    public static String getPermission(String api){
        String apiPrefix = api.split("\\?")[0];
        if (apiPrefix.startsWith("/")) {
            apiPrefix = apiPrefix.replaceFirst("/", "");
        }
        String permission = apiPrefix.replaceAll("/", ":");
        return permission;
        //apiPrefix.replaceAll("\\", ":");

    }

    public static String getLabel(Integer type){
        return map.get(type).getLabel();
    }
    public static String getLabel(String api){
        return getLabel(getType(api));
    }

}
